package yl.hs.bmipfull.service.laboratory.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.laboratory.ITaskConfigBackupDao;
import yl.hs.bmipfull.pojo.laboratory.ProductConfigDetailsEntity;
import yl.hs.bmipfull.pojo.laboratory.TaskConfigBackupEntity;
import yl.hs.bmipfull.service.laboratory.IProductConfigDetailsService;
import yl.hs.bmipfull.service.laboratory.IProductConfigService;
import yl.hs.bmipfull.service.laboratory.ITaskConfigBackupService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.datastate.ProductConfigType;

import java.util.Date;
import java.util.List;

/**
 * 生产任务配比备份信息service层实现类
 *
 * @author 张凯 on 2021/6/18 <br/>
 */
@Service
public class TaskConfigBackupService extends LaboratoryBaseService implements ITaskConfigBackupService {

    @Autowired
    ITaskConfigBackupDao backupDao;

    @Autowired
    IProductConfigDetailsService configDetailsService;

    @Autowired
    IProductConfigService configService;

    @Override
    public List<TaskConfigBackupEntity> queryBackByTaskSN(String taskSn) {
        return backupDao.queryBackByTaskSN(taskSn);
    }

    @Override
    public Integer deleteByTaskSN(String taskSn) {
        return backupDao.deleteByTaskSN(taskSn);
    }

    @Override
    public Integer saveConfigBack(TaskConfigBackupEntity backupEntity) {
        return backupDao.insertNew(backupEntity);
    }

    @Override
    public Integer saveConfigBack(String configSn, String taskSn) throws Exception {
        //将原有生产任务配比备份状态改为-1
        Integer result = 0;
        deleteByTaskSN(taskSn);
        //保存生产配比
        List<ProductConfigDetailsEntity> configDetailsList = configDetailsService.queryByConfigSn(configSn);
        for (ProductConfigDetailsEntity configDetails : configDetailsList) {
            TaskConfigBackupEntity backupEntity = new TaskConfigBackupEntity();
            configDetailsEntityToBackEntity(configDetails, backupEntity);
            backupEntity.setTaskSN(taskSn);
            backupEntity.setConfigType(String.valueOf(ProductConfigType.Production.code));

            result += saveConfigBack(backupEntity);
//            if (result > 0) {
//                writeLog(ActionType.CREATE, taskSn, "保存生产任务生产配比备份信息", backupEntity.toJson(), TaskConfigBackupEntity.class);
//            }
        }
        //保存理论配比
        String theoreticalSn = configService.findBySn(configSn).getTheoreticalSn();
        List<ProductConfigDetailsEntity> theoreticalConfigDetails = configDetailsService.queryByConfigSn(theoreticalSn);
        for (ProductConfigDetailsEntity configDetails : theoreticalConfigDetails) {
            TaskConfigBackupEntity backupEntity = new TaskConfigBackupEntity();
            configDetailsEntityToBackEntity(configDetails, backupEntity);
            backupEntity.setTaskSN(taskSn);
            backupEntity.setConfigType(String.valueOf(ProductConfigType.Theoretical.code));

            result += saveConfigBack(backupEntity);
//            if (result > 0) {
//                writeLog(ActionType.CREATE, taskSn, "保存生产任务生产理论备份信息", backupEntity.toJson(), TaskConfigBackupEntity.class);
//            }
        }
        return result;
    }

    void configDetailsEntityToBackEntity(ProductConfigDetailsEntity configDetails, TaskConfigBackupEntity backupEntity) {
        backupEntity.setRmCateSN(configDetails.getRmCate());
        backupEntity.setRmSN(configDetails.getRmSN());
        backupEntity.setRmName(configDetails.getRmName());
        backupEntity.setRmCate("");
        backupEntity.setRmSpec(configDetails.getRmSpec());
        backupEntity.setBrand(configDetails.getRmBrand());
        backupEntity.setQuantity(configDetails.getQuantity());
        backupEntity.setQuantityMin(configDetails.getQuantityMin());
        backupEntity.setQuantityMax(configDetails.getQuantityMax());
        backupEntity.setRate(configDetails.getRate());
        backupEntity.setBackTime(new Date());
        backupEntity.setState(DataState.CREATED.code);

    }
}
